<?php

/**
* @title Upload class - wrapper for uploading files. See accompanying docs
* @author C.Small
* @version 1.0
*
* More features and better error checking will come in the next version
* 
* @desc 21 May 2008
* @desc update
* @desc thanhnv
* @desc get and check file is allow file 
*/
class Upload
{
    private $maxupload_size;
    private $FILES;
    public $errors;
    
    private $aAllowImage = array('jpg', 'jpeg', 'gif', 'png','bmp');
    private $aAllowCompress = array('rar','zip','gz','tar');
    private $aAllowText = array('doc','xls','csv');
   
    public function __construct($_FILES)
    {
        $this->FILES = $_FILES;
        $this->isPosted = false;
        $this->maxupload_size = UPLOAD_MAX_FILE_SIZE;
    }
   
    public function save($directory, $field, $overwrite,$mode=0777)
    {
        $this->isPosted = true;
        if ($this->FILES[$field]['size'] < $this->maxupload_size && $this->FILES[$field]['size'] >0)
        {
            $noerrors = true;
            $this->isPosted = true;
            // Get names
            $tempName  = $this->FILES[$field]['tmp_name'];
            $file     = $this->FILES[$field]['name'];
            $all       = $directory.$file;

            // Copy to directory
            if (file_exists($all))
            {
               if ($overwrite)
             {
             @unlink($all)         || $noerrors=false;
             $this->errors  = "Upload class save error: unable to overwrite ".$all."<BR>";
             @copy($tempName,$all) || $noerrors=false;
             $this->errors .= "Upload class save error: unable to copy to ".$all."<BR>";
             @chmod($all,$mode)    || $ernoerrorsrors=false;
             $this->errors .= "Upload class save error: unable to change permissions for:
             ".$all."<BR>";
               }
            }else
            {
                @copy($tempName,$all)  || $noerrors=false;
                $this->errors  = "Upload class save error: unable to copy to ".$all."<BR>";
                @chmod($all,$mode)     || $noerrors=false;
                $this->errors .= "Upload class save error: unable to change permissions for:
                ".$all."<BR>";
            }
            return $noerrors;
        } elseif ($this->FILES[$field]['size'] > $this->maxupload_size)
         {
            $this->errors = "File size exceeds maximum file size of ".$this->maxuploadsize .
            " bytes";
            return false;
         } elseif ($this->FILES[$field]['size'] == 0)
            {
                $this->errors = "File size is 0 bytes";
                return false;
            }
    }
   
    public function saveAs($filename, $directory, $field, $overwrite,$mode=0777)
    {
        $this->isPosted = true;
        if ($this->FILES[$field]['size'] < $this->maxupload_size &&
            $this->FILES[$field]['size'] >0)
        {
            $noerrors = true;

            // Get names
            $tempName  = $this->FILES[$field]['tmp_name'];
            $all       = $directory.$filename;
           
            // Copy to directory
            if (file_exists($all))
            {
               if ($overwrite)
             {
             @unlink($all)         || $noerrors=false;
             $this->errors  = "Upload class saveas error: unable to overwrite ".$all."<BR>";
             @copy($tempName,$all) || $noerrors=false;
             $this->errors .= "Upload class saveas error: unable to copy to ".$all."<BR>";
             @chmod($all,$mode)    || $noerrors=false;
             $this->errors .= "Upload class saveas error: unable to copy to".$all."<BR>";
             }
            } else{
                @copy($tempName,$all)  || $noerrors=false;
                $this->errors  = "Upload class saveas error: unable to copy to ".$all."<BR>";
                @chmod($all,$mode)     || $noerrors=false;
                $this->errors .= "Upload class saveas error: unable to change permissions
                for: ".$all."<BR>";
            }
            return $noerrors;
        } elseif ($this->FILES[$field]['size'] > $this->maxupload_size)
         {
            $this->errors = "File size exceeds maximum file size of ".$this->maxuploadsize .
            " bytes";
            return false;
         } elseif ($this->FILES[$field]['size'] == 0)
            {
               $this->errors = "File size is 0 bytes";
               return false;
            }
     }
     
     /*****************Nam edit 280709***********/
     public function saveAs_OBJ($filename, $directory, $field_name,$field_size, $overwrite,$mode=0777)
    {
        $this->isPosted = true;
        if ($field_size < $this->maxupload_size &&
            $field_size >0)
        {
            $noerrors = true;

            // Get names
            $tempName  = $field_name;
            $all       = $directory.$filename;
           
            // Copy to directory
            if (file_exists($all))
            {
               if ($overwrite)
             {
             @unlink($all)         || $noerrors=false;
             $this->errors  = "Upload class saveas error: unable to overwrite ".$all."<BR>";
             @copy($tempName,$all) || $noerrors=false;
             $this->errors .= "Upload class saveas error: unable to copy to ".$all."<BR>";
             @chmod($all,$mode)    || $noerrors=false;
             $this->errors .= "Upload class saveas error: unable to copy to".$all."<BR>";
             }
            } else{
                @copy($tempName,$all)  || $noerrors=false;
                $this->errors  = "Upload class saveas error: unable to copy to ".$all."<BR>";
                @chmod($all,$mode)     || $noerrors=false;
                $this->errors .= "Upload class saveas error: unable to change permissions
                for: ".$all."<BR>";
            }
            return $noerrors;
        } elseif ($this->FILES[$field]['size'] > $this->maxupload_size)
         {
            $this->errors = "File size exceeds maximum file size of ".$this->maxuploadsize .
            " bytes";
            return false;
         } elseif ($field_size == 0)
            {
               $this->errors = "File size is 0 bytes";
               return false;
            }
     }
     /***************************************/
   
    public function getFileName($field)
    {
        return $this->FILES[$field]['name'];
    }
    
    public function getFileExtension($field){
    	$filename = $this->getFileName($field);
		return strtolower(substr($filename,strrpos($filename,'.')+1));
	}
	public function getFileExtension_OBJ($field){
    	//$filename = $this->getFileName($field);
		return strtolower(substr($field,strrpos($field,'.')+1));
	}
	public function isAllowFile($field){
		if(in_array($this->getFileExtension($field),$this->aAllowImage)){
			return true;
		}elseif(in_array($this->getFileExtension($field),$this->aAllowCompress)){
			return true;
		}elseif(in_array($this->getFileExtension($field),$this->aAllowText)){
			return true;
		}
		return false;
	}
	public function isAllowImageFile($field){
		return (in_array($this->getFileExtension($field),$this->aAllowImage)) ? true : false;
	}
	public function isAllowImageFile_OBJ($field){
		return (in_array($this->getFileExtension_OBJ($field),$this->aAllowImage)) ? true : false;
	}
	public function isAllowCompressFile($field){
		return (in_array($this->getFileExtension($field),$this->aAllowCompress)) ? true : false;
	}
	public function isAllowTextFile($field){
		return (in_array($this->getFileExtension($field),$this->aAllowText)) ? true : false;
	}
	
   
    public function getFileMimeType($field)
    {
        return $this->FILES[$field]['type'];
    }
   
    public function getFileSize($field)
    {
        return $this->FILES[$field]['size'];
    }

}
?>